package com.tec.security.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.tec.security.entity.AuditLog;
import com.tec.security.service.AuditLogService;
import com.tec.security.mapper.AuditLogMapper;
import com.tec.security.utils.date.DateString;
import com.tec.security.utils.other.LimitPage;
import com.tec.security.utils.security.entity.UserInformation;
import com.tec.security.utils.security.entity.UserKey;
import com.tec.security.utils.security.entity.Userlog;
import com.tec.security.utils.security.mapper.UserKeyMapper;
import com.tec.security.utils.security.service.UserInformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
* @author LL318
* @description 针对表【audit_log】的数据库操作Service实现
* @createDate 2023-12-01 01:11:27
*/
@Service
public class AuditLogServiceImpl extends ServiceImpl<AuditLogMapper, AuditLog>
    implements AuditLogService{
    private final UserInformationService userInformationService;
    private final UserKeyMapper userKeyMapper;
    private final AuditLogMapper auditLogMapper;
    @Autowired
    public AuditLogServiceImpl(UserInformationService userInformationService, UserKeyMapper userKeyMapper, AuditLogMapper auditLogMapper) {
        this.userInformationService = userInformationService;
        this.userKeyMapper = userKeyMapper;
        this.auditLogMapper = auditLogMapper;
    }

    @Override
    public void auditInfo(String loginID, String message, String type) {
        UserInformation userInformation=userInformationService.selectInformationByUUID(loginID);
        QueryWrapper<UserKey> userKeyQueryWrapper=new QueryWrapper<>();
        userKeyQueryWrapper.select("username").eq("uuid",loginID);
        AuditLog auditLog=new AuditLog();
        auditLog.setName(userInformation.getName());
        auditLog.setMessage(message);
        auditLog.setUsername(userKeyMapper.selectOne(userKeyQueryWrapper).getUsername());
        auditLog.setDate(DateString.currentDate());
        auditLog.setTime(DateString.currentTime());
        auditLog.setType(type);
        auditLogMapper.insert(auditLog);
    }

    @Override
    public Map<String, Object> allAuditLogList(LimitPage limitPage, String type) {
        QueryWrapper<AuditLog> auditLogQueryWrapper=new QueryWrapper<>();
        PageHelper.startPage(limitPage.getPage(), limitPage.getSize());
        Map<String,Object> result=new HashMap<>();
        QueryWrapper<AuditLog> auditLogCountQueryWrapper=new QueryWrapper<>();
        if(!Objects.equals(type, "全部")){
            auditLogQueryWrapper.eq("message",type);
            auditLogCountQueryWrapper.eq("message",type);
        }
        result.put("list",auditLogMapper.selectList(auditLogQueryWrapper));
        result.put("total",auditLogMapper.selectCount(auditLogCountQueryWrapper));
        return result;
    }
}




